<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/25 0025
 * Time: 9:19
 */
namespace app\adminapi\controller;

use app\adminapi\model\Api;
use app\api\model\JWT;
use think\App;
use think\Controller;
use think\Db;
use think\helper\Hash;
/**
 * @title 员工管理
 * @description 接口说明
 */
class Staff extends Api
{
    /**
     * @title 员工管理首页
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/staff/index
     * @method POST
     * @param name:page type:int require:1 default: other: desc:页码
     * @param name:name type:int require:0 default: other: desc:搜索昵称
     * 
     * @return list:列表@
     * @list id:id username:姓名 avatar:头像 mobiles:手机 mobile:账号 position:职位 role:权限 addtime:添加时间
     * @return page:总页数
     */
    public function index()
    {
        $user = $this->user;
        // print_R($user['id']);die;
        $page = input('page')??1;
        $search = input('name');
        if(is_numeric($search)){
            $where[] = ['mobiles','like',"%".$search."%"];
        }else{
            $where[] = ['username','like',"%".$search."%"];
        }
        $where[] = ['hid','=',$user['hid']];
        $list = db::name('user')->where($where)
        ->field('username,id,addtime,mobile,mobiles,avatar,role,position,sex')
        ->order('addtime desc')->page("$page,10")->select();
        $total = db::name('user')->where($where)->count();
        foreach ($list as $k => $v) {
            $list[$k]['avatar'] = get_file_path($v['avatar']);
            $rule = explode(',',$v['role']);
            foreach ($rule as $kr => $vr) {
                $role[] = db::name('admin_role')->where('id',$vr)->field('name')->find();
            }
            $list[$k]['role'] = $role;
            $role = [];
            $list[$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
        }
        $data['list'] = $list;
        $data['page'] = ceil($total/10);
        $data['pages'] = 10;
        $data['total'] = $total;
        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 员工管理导出
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/staff/banquet_export
     * @method POST
     */
    public function banquet_export()
    {
        $user = $this->user;
        // 查询数据
        $where[] = ['hid','=',$user['hid']];
        // $where[] = ['status','=',3];
        $director = db::name('user')->where('hid',$user['hid'])->field('id,username')->select();
        if($director){
            foreach ($director as $k => $v) {
                $where[] = ['manager_id','=',$v['id']];
                $director[$k]['count'] = db::name('check_order')->where($where)->count();
                $director[$k]['desk'] = db::name('check_order')->where($where)->sum('desk');
                $director[$k]['total_money'] = db::name('check_order')->where($where)->sum('total_money');
            }
        }
        // 设置表头信息（对应字段名,宽度，显示表头名称）
        $cellName = [
            ['id', 'auto', 'ID'],
            ['username', 'auto', '客户经理'],
            ['count', 'auto', '成功订单'],
            ['desk', 'auto', '总桌数'],
            ['total_money', 'auto', '总金额'],
        ];
        // 调用插件（传入插件名，[导出文件名、表头信息、具体数据]）
        plugin_action('Excel/Excel/export', ['客户经理排行', $cellName, $director]);
    }
    /**
     * @title 添加员工获取角色
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/staff/get_rule
     * @method POST
     * 
     * @return role:列表@
     * @role id:id name:名称
     * 
     */
    public function get_rule()
    {
        $user = $this->user;
        $role = db::name('admin_role')->where('hid',$user['hid'])->field('id,name')->select();
        $data['role'] = $role;
        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 添加员工保存
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/staff/save
     * @method POST
     * @param name:id type:int require:0 default: other: desc:修改id
     * @param name:username type:int require:1 default: other: desc:姓名
     * @param name:sex type:int require:1 default: other: desc:性别1男2女
     * @param name:mobiles type:int require:1 default: other: desc:手机
     * @param name:position type:int require:1 default: other: desc:职位
     * @param name:mobile type:int require:1 default: other: desc:账号
     * @param name:password type:int require:1 default: other: desc:密码
     * @param name:role type:int require:1 default: other: desc:角色
     * 
     * 
     */
    public function save()
    {
        $user = $this->user;
        if(!input('username') || !input('sex') || !input('mobiles') || !input('position') || !input('mobile') || !input('role')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $id = input('id');
        $res['username'] = input('username');
        $res['sex'] = input('sex');
        $res['mobiles'] = input('mobiles');
        $res['position'] = input('position');
        $res['mobile'] = input('mobile');
        $res['role'] = input('role');
        $password = input('password');

        if($id){
            if($password){
                $res['password'] = $this->setPasswordAttr($password);
            }
            if(db::name('user')->where('id',$id)->update($res)){
                return $this->ajaxReturn(0,'成功');
            }
        }else{
            $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
            $randStr = str_shuffle($str);//打乱字符串
            $rands= substr($randStr,0,6);//substr(string,start,length);返回字符串的一部分
            $res['password'] = $this->setPasswordAttr($password);
            $res['num'] = $rands ;
            $res['addtime'] = time();
            $res['hotel'] = $user['username'];
            $res['hid'] = $user['hid'];
            if(db::name('user')->insert($res)){
                return $this->ajaxReturn(0,'成功');
            }
        }
        
    }
      /**
     * @title 修改员工获取数据
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/staff/update
     * @method POST
     * @param name:id type:int require:1 default: other: desc:员工id
     * 
     * @return list:列表@
     * 
     * @list id:id username:姓名 sex:1男2女 mobiles:手机号 position:职位 role:角色 mobile:账号
     * 
     */
    public function update()
    {
        $user = $this->user;
        $id = input('id');
        if(!$id){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $list = db::name('user')->where('id',$id)->field('id,username,sex,mobiles,position,role,mobile')->find();
        if($list['role']){
            $list['role']= explode(',',$list['role']);
        }
        return $this->ajaxReturn(0,'成功',$list);

    }
    /**
     * @title 查看员工的订单
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/staff/say
     * @method POST
     * @param name:id type:int require:1 default: other: desc:员工id
     * 
     * 
     */
    public function say()
    {
        $user = $this->user;
        $id = input('id');
        if(!$id){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $scheduled = db::name('scheduled')->where('director_id',$id)->where('type','neq',3)->where('hid',$user['hid'])->select();
        $zero = db::name('zero')->where('director_id',$id)->where('status','lt',3)->where('hid',$user['hid'])->select();
        if($scheduled || $zero){
            return $this->ajaxReturn(1,'此员工有订单未完成，请先进行离职交接哦');
        }else{
            return $this->ajaxReturn(0,'成功');
        }
    }

    /**
     * @title 删除员工
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/staff/del_user
     * @method POST
     * @param name:id type:int require:1 default: other: desc:员工id
     * 
     * 
     */
    public function del_user()
    {
        $user = $this->user;
        $id = input('id');
        if(!$id){
            return $this->ajaxReturn(1,'缺少参数');
        }
        // $scheduled = db::name('scheduled')->where('director_id',$id)->where('type','neq',3)->where('hid',$user['hid'])->select();
        // $zero = db::name('zero')->where('director_id',$id)->where('status','lt',3)->where('hid',$user['hid'])->select();
        // if($scheduled || $zero){
        //     return $this->ajaxReturn(1,'此员工有订单未完成，请先进行离职交接哦');
        // }
        db::name('user')->where('id',$id)->delete();
        return $this->ajaxReturn(0,'成功');

    }

     // 对密码进行加密
 public function setPasswordAttr($value)
 {
      return Hash::make((string)$value);
 }

    
}